package com.xqb.modules.controller.ins;

import com.alibaba.fastjson2.JSON;
import com.xqb.common.annotation.Log;
import com.xqb.common.constants.MQConstants;
import com.xqb.common.core.controller.BaseController;
import com.xqb.common.core.domain.AjaxResult;
import com.xqb.common.core.page.TableDataInfo;
import com.xqb.common.enums.BusinessType;
import com.xqb.common.utils.DateUtils;
import com.xqb.common.utils.poi.ExcelUtil;
import com.xqb.modules.domain.po.InsInstitution;
import com.xqb.modules.domain.po.InsReview;
import com.xqb.modules.service.IInsInstitutionService;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 机构Controller
 * 
 * @author xqb
 * @date 2024-10-14
 */
@RestController
@RequestMapping("/insititution/institution")

public class InsInstitutionController extends BaseController
{
    @Autowired
    private IInsInstitutionService insInstitutionService;

    @Autowired
    private RabbitTemplate rabbitTemplate;
    /**
     * 查询机构列表
     */
    @PreAuthorize("@ss.hasPermi('insititution:institution:list')")
    @GetMapping("/list")
    public TableDataInfo list(InsInstitution insInstitution)
    {
        startPage();
        List<InsInstitution> list = insInstitutionService.selectInsInstitutionList(insInstitution);
        return getDataTable(list);
    }
    /**
     * 导出机构列表
     */
    @PreAuthorize("@ss.hasPermi('insititution:institution:export')")
    @Log(title = "机构", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, InsInstitution insInstitution)
    {
        List<InsInstitution> list = insInstitutionService.selectInsInstitutionList(insInstitution);
        ExcelUtil<InsInstitution> util = new ExcelUtil<InsInstitution>(InsInstitution.class);
        util.exportExcel(response, list, "机构数据");
    }

    /**
     * 获取机构详细信息
     */
    @PreAuthorize("@ss.hasPermi('insititution:institution:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(insInstitutionService.selectInsInstitutionById(id));
    }

    /**
     * 新增机构
     */
    @PreAuthorize("@ss.hasPermi('insititution:institution:add')")
    @Log(title = "机构", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody InsInstitution insInstitution)
    {
        return toAjax(insInstitutionService.insertInsInstitution(insInstitution));
    }

    /**
     * 修改机构
     */
    @PreAuthorize("@ss.hasPermi('insititution:institution:edit')")
    @Log(title = "机构", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody InsInstitution insInstitution)
    {
        return toAjax(insInstitutionService.updateInsInstitution(insInstitution));
    }

    /**
     * 删除机构
     */
    @PreAuthorize("@ss.hasPermi('insititution:institution:remove')")
    @Log(title = "机构", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(insInstitutionService.deleteInsInstitutionByIds(ids));
    }


    /**
     * 机构审核并向申请人发送邮件消息
     */
    @PostMapping("/review")
    public AjaxResult review(@RequestBody InsReview insReview) {
        //2024-10-14 16:00:00,当前时间转String
        insReview.setReviewTime(DateUtils.getTime());

        rabbitTemplate.convertAndSend(MQConstants.EMAIL_TOPIC, MQConstants.EMAIL_INSTITUTION_KEY, JSON.toJSONString(insReview));
        return AjaxResult.success("处理成功");
    }

}
